package com.sfj.system.service.task;

import com.sfj.common.exception.CheckException;
import com.sfj.system.dto.task.ProblemCheckDTO;
import com.sfj.system.dto.task.ProcessReviewDTO;
import com.sfj.system.dto.task.TaskGetQueryDTO;
import com.sfj.system.dto.task.TaskProblemDescDTO;
import com.sfj.system.entity.ProblemProcessEntity;
import com.sfj.system.entity.TaskProblemEntity;
import com.sfj.system.enums.ProblemStatusEnum;
import com.sfj.system.enums.TaskStatusEnum;
import com.sfj.system.mapper.ProblemMapper;
import com.sfj.system.mapper.TaskMapper;
import com.sfj.system.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @author kaka
 * @Date 2025/07/02
 */
@Service
public class ProblemService {
    @Resource
    private ProblemMapper problemMapper;
    @Autowired
    private ProblemProcessService problemProcessService;
    @Autowired
    private TaskService taskService;
    @Resource
    private TaskMapper taskMapper;

    public void insert(TaskProblemEntity taskProblemEntity){
        problemMapper.insertProblem(taskProblemEntity);
    }
    public void del(Integer taskId){
        problemMapper.deleteProblemsByTaskId(taskId);
    }

    public void batchSave(List<TaskProblemEntity> problems){
        problemMapper.batchInsertProblems(problems);
    }

    public List<TaskProblemEntity> select(Integer taskId){
        List<TaskProblemEntity> problemEntities = problemMapper.selectProblemsByTaskId(taskId);
        return problemEntities;
    }
    public List<TaskProblemDescDTO> selectDesc(Integer taskId,Integer userId){
        List<TaskProblemDescDTO> problemEntities = problemMapper.selectProblemsDescByTaskId(taskId,userId);
        return problemEntities;
    }

    public void updStatus(Integer problemId){
        TaskProblemEntity problem = new TaskProblemEntity();
        problem.setProblemId(problemId);
        problem.setStatus(ProblemStatusEnum.RECTIFIED.getCode());
        problemMapper.updateStatus(problem);
    }

    //查询反馈列表
    public List<ProblemCheckDTO> queryReview(Integer taskId){
        List<ProblemCheckDTO> problemEntities = problemProcessService.getProblemProcessesByTaskId(taskId);
        return problemEntities;
    }

    //任务复核
    @Transactional
    public String processReview(ProcessReviewDTO processReviewDTO) {

        ProblemProcessEntity problemProcessEntity = problemProcessService.getProblemProcessById(processReviewDTO.getProblemId(),
                processReviewDTO.getTaskId(),
                processReviewDTO.getRfUser());
        if(problemProcessEntity ==null ){
            throw new CheckException("该反馈记录不存在");
        }
        TaskGetQueryDTO taskEntity = taskMapper.selectById(problemProcessEntity.getTaskId());
        //同意
        if(processReviewDTO.isReview()){
            problemProcessEntity.setStatus(TaskStatusEnum.REVIEWED.getCode());
            problemProcessService.updStatus(problemProcessEntity);
            //判断当前整改问题都已复核完毕
            if(problemProcessService.countByNotReviewed(problemProcessEntity.getTaskId(),problemProcessEntity.getRfUser())==0){
                String rwUsers = StringUtils.addIdToCommaString(taskEntity.getRwUsers(), String.valueOf(problemProcessEntity.getRfUser()));
                taskEntity.setRwUsers(rwUsers);
                String rwUnits = StringUtils.addIdToCommaString(taskEntity.getRwUnits(), String.valueOf(problemProcessEntity.getRfUnit()));
                taskEntity.setRwUnits(rwUnits);
                //更新部分已复核用来下一个人反馈有新任务复核提醒
                taskEntity.setStatus(TaskStatusEnum.PENDING_REVIEWED.getCode());
                taskMapper.updReviewAndStatus(taskEntity);
            }

            //所有任务都已复核即任务已完成
            if (StringUtils.isSameElements(taskEntity.getRfUsers(),taskEntity.getRwUsers())) {
                taskService.taskFinish(problemProcessEntity.getTaskId());
                return "finish";
            }
            // 驳回
        }else {
            problemProcessEntity.setMessage(processReviewDTO.getMsg());
            problemProcessEntity.setStatus(TaskStatusEnum.REJECT.getCode());

            //添加驳回人员
            String rjUsers = StringUtils.addIdToCommaString(taskEntity.getRjUsers(), String.valueOf(processReviewDTO.getRfUser()));
            taskEntity.setRjUsers(rjUsers);
            taskMapper.updProcessAndStatus(taskEntity);
            problemProcessService.updStatus(problemProcessEntity);
        }


        return null;
    }
}
